<template>
  <div class="send-record">
    <div class="send-record-wrapper-bg">
      <img
        src="http://qiniu.whxgjt.com/dfh2/img_wdzf.png"
        alt=""
        class="img-wdzf"
      />
    </div>

    <div class="send-record-wrapper">
      <ul class="send-record-ul">
        <li v-for="item in messageList" :key="item.id">
          <span>{{ item.msg }}</span>
          <span @click="deleteMessage(item.id)" class="record-detele"
            >删除</span
          >
        </li>
      </ul>
    </div>

    <div class="form-wrapper">
      <div class="form">
        <textarea
          placeholder="想想说什么呢....."
          rows="2"
          maxlength="100"
          v-model="message"
          @focus="inputFocus"
          @blur="inputBlur"
        ></textarea>

        <div>
          <img
            src="~@/assets/img/record/btn_fs.png"
            alt=""
            @click="addMessage"
            class="btn-send"
          />
          <img
            src="~@/assets/img/record/btn_fh.png"
            alt=""
            @click="back"
            class="btn-back"
          />
        </div>
      </div>
    </div>

    <img
      src="http://qiniu.whxgjt.com/dfh2/ip_04.png"
      alt=""
      class="ip-04"
      v-show="showIp"
    />

    <div class="footer">
      我们将随机抽取20条幸运弹幕，发送东方小红抱枕*1个，发完弹幕记得留下你的收货地址哦
    </div>
  </div>
</template>

<script>
import { WxShare } from "@/utils/wxConfig";
import { getUserMessage, addMessage, deleteMessage } from "@/api/common";

export default {
  name: "SendRecord",
  data() {
    return {
      showIp: true,
      message: "",
      messageList: [],
    };
  },
  methods: {
    async getUserMessage() {
      const openid = localStorage.getItem("openid");
      if (!openid) return;
      this.$showLoading();
      try {
        const res = await getUserMessage(openid);
        const data = res.data;
        if (data.errorCode === 0) {
          const rows = data.data;
          this.messageList = rows;
        } else {
          this.messageList = [];
        }
      } catch (e) {
        console.log(e);
        this.messageList = [];
      }
      this.$closeLoading();
    },
    async addMessage() {
      const openid = localStorage.getItem("openid");
      if (!openid) return;
      if (!this.message) return;
      this.$showLoading();
      try {
        const res = await addMessage({
          userId: openid,
          message: this.message,
        });
        const data = res.data;
        if (data.errorCode === 0) {
          this.getUserMessage();
          this.message = "";
        }
      } catch (e) {
        console.log(e);
      }
      this.$closeLoading();
    },
    async deleteMessage(id) {
      const openid = localStorage.getItem("openid");
      if (!openid) return;
      this.$showLoading();
      try {
        const res = await deleteMessage({
          messageId: id,
          userId: openid,
        });
        const data = res.data;
        if (data.errorCode === 0) {
          this.getUserMessage();
        }
      } catch (e) {
        console.log(e);
      }
      this.$closeLoading();
    },
    back() {
      this.$router.push("/");
    },
    inputFocus() {
      // this.showIp = false;
    },
    inputBlur() {
      // this.showIp = true;
    },
  },
  mounted() {
    WxShare();
    this.getUserMessage();
  },
};
</script>

<style lang="scss" scoped>
.send-record {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  overflow: hidden;
  background-image: url("http://qiniu.whxgjt.com/dfh2/bg_dt.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  box-sizing: border-box;
  position: relative;

  .send-record-wrapper-bg {
    width: 100%;
    text-align: center;
    position: absolute;
    top: 53px;
    z-index: 9;
    .img-wdzf {
      height: 480px;
    }
  }

  .send-record-wrapper {
    width: 100%;
    height: 140px;
    padding: 0 40px;
    position: absolute;
    top: 150px;
    box-sizing: border-box;
    z-index: 10;
    .send-record-ul {
      height: 100%;
      margin: 0;
      overflow: auto;
      li {
        margin-bottom: 20px;
        font-size: 15px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #b0612d;
        &:last-child {
          margin-bottom: 0;
        }
      }
      .record-detele {
        margin-left: 10px;
        font-size: 13px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #7b6b61;
        border-bottom: 1px solid #7b6b61;
      }
    }
  }

  .form-wrapper {
    width: 100%;
    height: 169px;
    padding: 0 61px;
    text-align: center;
    position: absolute;
    top: 332px;
    box-sizing: border-box;
    z-index: 10;
    .form {
      width: 100%;
      height: 100%;
      background-color: rgba(255, 228, 208, 1);
      border-radius: 8px;
    }
    textarea {
      width: 100%;
      height: 120px;
      padding: 10px;
      font-size: 15px;
      outline-style: none;
      border: 0px;
      background-color: transparent;
      box-sizing: border-box;
      &::-webkit-input-placeholder {
        color: #b0612d;
      }
      &::-moz-placeholder {
        color: #b0612d;
      }
    }
    .btn-send {
      height: 33px;
    }
    .btn-back {
      height: 33px;
      padding-left: 13px;
    }
  }

  .ip-04 {
    width: 100%;
    height: 112px;
    position: fixed;
    bottom: 0;
  }

  .footer {
    position: relative;
    font-size: 12px;
    padding: 20px;
    margin-top: 580px;
    line-height: 20px;
    color: #fff;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 10;
  }
}

@media only screen and (min-height: 700px) {
  .footer {
    position: fixed !important;
    bottom: 0 !important;
  }
}
</style>